package org.wak.inventory
{
	import flash.events.EventDispatcher;
	
	import org.wak.inventory.events.InventoryEvent;
	import org.wak.utils.ArrayIterator;
	import org.wak.utils.IIterator;
	
	/**
	 * Inventory class. 
	 * 
	 */		
	public class Inventory extends EventDispatcher
	{
		// array of items stored in the inventory.
		private var mItemsArray:Array = null;
		
		public function Inventory()
		{
			mItemsArray = new Array();
		}
		
		/**
		 * Adds new item to the inventory 
		 * @param pItem
		 * 
		 */		
		public function AddItem(pItem:InventoryItem):void
		{
			if(this.HasItem(pItem) )
				return ;
			
			//trace("AddItem: "+ pItem.interactableId);
			mItemsArray.push(pItem);
			
			var fEvent:InventoryEvent = new InventoryEvent(InventoryEvent.ITEM_ADDED, pItem);
			this.dispatchEvent( fEvent );
			
		}
		
		/**
		 * Removes an Item from the inventory 
		 * @param pItem
		 * 
		 */		
		public function RemoveItem(pItem:InventoryItem):void
		{
			var fCurItem:InventoryItem = null ;
			var fElemIndex:int = mItemsArray.indexOf(pItem);
			
			if( fElemIndex >= 0 )
				mItemsArray.splice( fElemIndex,1);
				
			var fEvent:InventoryEvent = new InventoryEvent(InventoryEvent.ITEM_REMOVED, pItem);
			this.dispatchEvent( fEvent );
						
		}
		
		/**
		 * Test if this inventory contains an item. 
		 * @param pItem
		 * @return true if the inventory has pItem
		 * 
		 */		
		public function HasItem( pItem:InventoryItem ):Boolean
		{
			var fRet:Boolean = false ;
			
			var fElemIndex:int = mItemsArray.indexOf(pItem);
			
			if(fElemIndex >= 0)
				fRet = true ;
			
			return fRet ;
		}
		
		/**
		 * Get an iterator of InventoryItems 
		 * @return 
		 * 
		 */		
		public function GetItems():IIterator
		{
			return new ArrayIterator( mItemsArray );
		}

	}
}